/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of LModeller.
 *
 * LModeller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * LModeller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LModeller.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.lmodeller.blocks;

import java.util.ArrayList;

import org.lwjgl.util.vector.Vector3f;



public class BlockModel {
	ArrayList<Block> blocks = new ArrayList<Block>();
	Vector3f location = new Vector3f();
	Vector3f size = new Vector3f();
	String name;
	
	public void addBlock(Block block) {
		blocks.add(block);
	}

	public ArrayList<Block> getBlocks() {
		return blocks;
	}

	public Vector3f getLocation()
	{
		return location;
	}

	public Vector3f getSize()
	{
		return size;
	}

	public void setName(String name) {
		this.name = name;
		
	}
	
	public String getName()
	{
		return name;
	}


	/**
	 * define location and size
	 */
	public void processModel() {
		Vector3f maximum = new Vector3f();
		for(int i=0;i<blocks.size();i++)
		{
			Block block = blocks.get(i);
			if(i==0)
			{
				if(block.rotation==0)
				{
					location.set(block.getX()-block.getLenght()/2f,
							block.getY()-block.getHeight()/2f,
							block.getZ()-block.getWidth()/2f);
					maximum.set(block.getX()+block.getLenght()/2f,
							block.getY()+block.getHeight()/2f,
							block.getZ()+block.getWidth()/2f);
				}
				else
				{
					location = new Vector3f(block.getX()-block.getWidth()/2f,
							block.getY()-block.getHeight()/2f,
							block.getZ()-block.getLenght()/2f);
					maximum = new Vector3f(block.getX()+block.getWidth()/2f,
							block.getY()+block.getHeight()/2f,
							block.getZ()+block.getLenght()/2f);
				}
			}
			if(block.rotation==0)
			{
				if(block.getX()-block.getLenght()/2f < location.x)
					location.x = block.getX()-block.getLenght()/2f;
				if(block.getY()-block.getHeight()/2f < location.y)
					location.y = block.getY()-block.getHeight()/2f;
				if(block.getZ()-block.getWidth()/2f < location.z)
					location.z = block.getZ()-block.getWidth()/2f;

				if(block.getX()+block.getLenght()/2f > maximum.x)
					maximum.x = block.getX()+block.getLenght()/2f;
				if(block.getY()+block.getHeight()/2f > maximum.y)
					maximum.y = block.getY()+block.getHeight()/2f;
				if(block.getZ()+block.getWidth()/2f > maximum.z)
					maximum.z = block.getZ()+block.getWidth()/2f;
			}
			else
			{
				if(block.getX()-block.getWidth()/2f < location.x)
					location.x = block.getX()-block.getWidth()/2f;
				if(block.getY()-block.getHeight()/2f < location.y)
					location.y = block.getY()-block.getHeight()/2f;
				if(block.getZ()-block.getLenght()/2f < location.z)
					location.z = block.getZ()-block.getLenght()/2f;

				if(block.getX()+block.getWidth()/2f > maximum.x)
					maximum.x = block.getX()+block.getWidth()/2f;
				if(block.getY()+block.getHeight()/2f > maximum.y)
					maximum.y = block.getY()+block.getHeight()/2f;
				if(block.getZ()+block.getLenght()/2f > maximum.z)
					maximum.z = block.getZ()+block.getLenght()/2f;
			}
		}
		size.set(maximum.x - location.x,
				maximum.y - location.y,
				maximum.z - location.z);
	}
}
